package cn.xq.pethome.cvCode;

import cn.xq.pethome.utils.AjaxUtil;
import cn.xq.pethome.utils.StrUtils;
import cn.xq.pethome.utils.VerifyCodeUtils;
import io.swagger.annotations.Api;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.lang.model.element.VariableElement;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/vcode")
@Api(value ="vcode",description="验证码")
public class CodeController {
    private RedisTemplate redisTemplate;//redis
    @GetMapping("/imgcode/{key}")
    public AjaxUtil getImgCode(@PathVariable("key") String key){
        try {
            String code = StrUtils.getRandomString(6).toLowerCase();
            redisTemplate.opsForValue().set(key,code,3, TimeUnit.MINUTES);
            String s = VerifyCodeUtils.VerifyCode(80, 30, code);
            return AjaxUtil.success().setData(s);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("图片验证码失败！");
            return AjaxUtil.error();
        }
    }
}
